package com.example.web.controller;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.outstanding.framework.container.springmvc.WebLogUtils.*;

import java.util.List;
import java.util.Map;

import com.example.web.api.CodeItem;
import com.example.web.api.DemoReqDto;
import com.example.web.api.QueryData;
import com.example.web.api.ReqPageDto;

import com.example.web.bo.DemoBO;
import com.example.web.bo.Dog;
import com.example.web.bo.Home;
import com.example.web.bo.Person;
import com.example.web.bo.Sex;
import com.example.web.dao.DemoDAO;
import com.example.web.service.DemoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.outstanding.framework.base.TransactionLogger;
import com.outstanding.framework.container.springmvc.WebDTO;
import com.outstanding.framework.core.PagesResponseDTO;
import com.outstanding.framework.core.PendingException;
import com.outstanding.framework.core.ResponseDTO;

@Controller
@RequestMapping("/")
public class DemoController {

	@Autowired
	private DemoService demoService;

	@Autowired
	private DemoDAO dao;

	public DemoDAO getDao() {
		return dao;
	}

	public void setDao(DemoDAO dao) {
		this.dao = dao;
	}

	/**
	 * Title: query<br/>
	 * Description: get post请求都会响应<br/>
	 * 
	 * @author ousei
	 * @date 2014年12月15日下午9:05:03
	 *
	 * @return ${返回信息描述}
	 * @throws PendingException
	 * @throws Exception
	 */
	@RequestMapping(value = "/query.do")
	public @ResponseBody List query() throws PendingException {
		debug("调用query");
		List<Home> res = this.demoService.query();

		return res;
	}

	/**
	 * Title: query<br/>
	 * Description: get post请求都会响应<br/>
	 * 
	 * @author ousei
	 * @date 2014年12月15日下午9:05:03
	 *
	 * @return ${返回信息描述}
	 * @throws PendingException
	 * @throws Exception
	 */
	@RequestMapping(value = "/querydao.do")
	public @ResponseBody List queryByDao() throws PendingException {
		debug("调用querydao");
		List<Home> res = this.dao.query();

		return res;
	}

	/**
	 * get post请求都会响应
	 * 
	 * @param session
	 * @return
	 * @throws PendingException
	 */
	// 演示http无参数情况
	// 演示http无参数情况，且需要访问session的情况
	@RequestMapping(value = "/set.do")
	public @ResponseBody ResponseDTO setsession(HttpSession session) throws PendingException {

		debug("调用queryx");
		debug("请求的Session ID＝" + session.getId());
		TransactionLogger.debug("调用set");
		session.setAttribute("a", "dsa");
		// 如果只需要回一个成功指令
		return ResponseDTO.creatDTO("dsa");

	}

	// 演示http无参数情况
	// 演示http无参数情况，且需要访问session的情况
	@RequestMapping(value = "/get.do")
	public @ResponseBody String get(HttpSession session) throws PendingException {

		debug("调用queryx");
		debug("请求的Session ID＝" + session.getId());
		TransactionLogger.debug("调用get");
		String str = (String) session.getAttribute("a");
		return str;
	}

	/**
	 * 只会响应POST请求
	 * 
	 * @param reqDto
	 * @return
	 * @throws PendingException
	 */
	// 演示http有POST请求参数情况
	// 演示http有POST请求参数情况
	@RequestMapping(value = "/querybyidd.do")
	public @ResponseBody Home query(@RequestBody DemoReqDto reqDto) throws PendingException {
		if (reqDto == null || !reqDto.validate()  ) {
			debug("reqDto 为空");
			PendingException ex = new PendingException(CodeItem.Demo_INPUTERROR, "reqDto 为空");
			String data = "返回的数据";
			ex.setData(data);
			throw ex;
		}

		Home bo = new Home();
		bo.setId(reqDto.getId());
		Home res = this.dao.querybyid(bo);
		return res;
	}

	/**
	 * get 请求会响应,id的值通过get传递过来
	 * 
	 * @param id
	 * @return
	 * @throws PendingException
	 */
	// 演示http有get请求参数情况
	@RequestMapping(value = "/q.do")
	public @ResponseBody Home q(String id) throws PendingException {
		if (StringUtils.isBlank(id)) {
			debug("id 为空");
			PendingException ex = new PendingException(CodeItem.Demo_INPUTERROR2, "reqDto 为空");
			String data = "返回的数据";
			ex.setData(data);
			throw ex;
		}

		Home bo = new Home();
		long longid = Long.parseLong(id);
		bo.setId(longid);
		Home res = this.dao.querybyid(bo);
		return res;
	}

	// 演示http有分页的请求
	@RequestMapping(value = "/fenye.do")
	public @ResponseBody ResponseDTO fenye(@RequestBody ReqPageDto dto) throws PendingException {

		DemoBO bo = new DemoBO();
		bo.setMsg_id(dto.getId());
		List<Home> res = this.demoService.fenye(dto.getPageNo(), dto.getPageSize(), bo);
		ResponseDTO pageInfo = ResponseDTO.creatPagesDTO(res);
		return pageInfo;

	}

	// 演示http有分页的请求
	@RequestMapping(value = "/fenye3.do")
	public @ResponseBody PageInfo fenye2(@RequestBody ReqPageDto dto) throws PendingException {
		DemoBO bo = new DemoBO();
		bo.setMsg_id(dto.getId());
		PageInfo res = this.demoService.fenye2(dto.getPageNo(), dto.getPageSize(), bo);
		return res;

	}

	// 演示通用DTO的请求
	@RequestMapping(value = "/dto.do")
	public @ResponseBody ResponseDTO dto(@RequestBody WebDTO dto) throws PendingException {
		DemoBO bo = dto.createBO(DemoBO.class);
		debug("bo的属性msg_id:" + bo.getMsg_id());
		debug("bo的属性i:" + bo.getI());
		debug("bo的属性l:" + bo.getL());
		debug("bo的属性bool:" + bo.getBool());
		debug("bo的属性b:" + bo.isB());
		debug("bo的属性money:" +bo.getMoney());
		debug("bo的属性ii:" +bo.getIi().toString());
		return ResponseDTO.creatDTO(bo);

	}

	// 演示通用DTO2的请求,是一个数组
	@RequestMapping(value = "/dto2.do")
	public @ResponseBody ResponseDTO dto2(@RequestBody WebDTO dto) throws PendingException {
		List list = dto.creatArrayBO(DemoBO.class);
		for (int i = 0; i < list.size(); i++) {
			DemoBO bo = (DemoBO)list.get(i);
			debug("bo的属性msg_id:" + bo.getMsg_id());
			debug("bo的属性i:" + bo.getI());
			debug("bo的属性l:" + bo.getL());
			debug("bo的属性bool:" + bo.getBool());
			debug("bo的属性b:" + bo.isB());
			debug("bo的属性money:" +bo.getMoney());
			debug("bo的属性ii:" +bo.getIi());
		}
		
		return ResponseDTO.creatDTO(list);

	}

	// 演示通用DTO的请求,属性中还有关联和枚举
	@RequestMapping(value = "/dto3.do")
	public @ResponseBody ResponseDTO dto3(@RequestBody WebDTO dto) throws PendingException {
		Person person = dto.createBO(Person.class);
		String name = person.getName();
		Dog dog = person.getDog();
		Sex sex = person.getSex();
		debug("person的属性name:" + person.getName());
		debug("person的dog的name:" +dog.getName());
		debug("person的sex:" + sex.getName());
		
		return ResponseDTO.creatDTO(person);
	}

		// 演示通用DTO的请求,属性中还有关联  多对一的情况
		@RequestMapping(value = "/dto4.do")
		public @ResponseBody ResponseDTO dto4(@RequestBody WebDTO dto) throws PendingException {
			Home home = dto.createBO(Home.class);
			List<Person> persons = home.getPersons();
			for(Person person : persons){
				Dog dog = person.getDog();
				Sex sex = person.getSex();
				debug("person的属性name:" + person.getName());
				if(dog != null)
					debug("person的dog的name:" +dog.getName());
				debug("person的sex:" + sex.getName());
			}
			return ResponseDTO.creatDTO(home);
		}

	

	/**
	 * Sets demo service.
	 *
	 * @param demoService
	 *            the demo service
	 */
	public void setDemoService(DemoService demoService) {
		this.demoService = demoService;
	}
}
